package com.hddz.acquisition.activity.stock;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.hddz.acquisition.AcquistionApp;
import com.hddz.acquisition.AppSetting;
import com.hddz.acquisition.DialogJst;
import com.hddz.acquisition.R;
import com.hddz.acquisition.SmartCallBack;
import com.hddz.acquisition.SoundUtil;
import com.hddz.acquisition.XUtil;
import com.hddz.acquisition.model.Command;
import com.hddz.acquisition.model.JsonRespose;
import com.hddz.acquisition.view.DialogHelper;
import com.hddz.acquisition.view.ProductOutAdapter;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_stock_armshaft_list)
public class ProductOutStockListActivity extends FragmentActivity {

	@ViewInject(R.id.list)
	private ListView list;

	@ViewInject(R.id.lbl_main_title)
	private TextView lbl_main_title;
	ProductOutAdapter adapter = null;

	String GUID = java.util.UUID.randomUUID().toString();
	@SuppressLint("SimpleDateFormat")
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


	private SoundUtil soundUtil;

	private DialogHelper dialogHelper;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		x.view().inject(this);

		dialogHelper = new DialogHelper(this);
		soundUtil = new SoundUtil(this, true, false);

		lbl_main_title.setText(R.string.menu_bpqd);
		adapter = new ProductOutAdapter(this);
		list.setAdapter(adapter);
		list.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				// TODO Auto-generated method stub
				AlertDialog.Builder ibuilder = new AlertDialog.Builder(
						ProductOutStockListActivity.this);
				ibuilder.setTitle("提示");
				ibuilder.setMessage("是否删除此条记录？");
				ibuilder.setPositiveButton(R.string.remove,
						new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								com.alibaba.fastjson.JSONObject item = adapter.getItem(position);
								sendCommand("1708", item.getString("cCode"));
								dialog.dismiss();
							}
						});
				ibuilder.setNegativeButton(R.string.cancel, null);
				ibuilder.create().show();

				return false;
			}
		});
		sendCommand("1709", GUID);
	}

	@Event(value =R.id.btn_back)
	private void onBack(View view) {
		finish();
	}

	private void sendCommand(final String cmd1, final String guid) {
		String url = "http://"
				+ AppSetting.getAppSetting(AcquistionApp.getInstance()).SERVER_ADDRESS
						.get() + "/HoldHandler.ashx";
		Command command = new Command();
		command.command = cmd1;
		command.guid = guid;
		command.datetime = format.format(new Date());
		command.barcode = "";
		command.holdname = AppSetting.getAppSetting(getApplicationContext()).UN
				.get();
		command.sourcewarehouse = "";
		command.targetwarehouse = "";
		command.invcode = "";
		command.free1 = "";
		command.importtype = AppSetting.getAppSetting(getApplicationContext()).IMPORT_TYPE
				.get();
		command.vouchtype = AppSetting.getAppSetting(getApplicationContext()).VOUCH_TYPE
				.get();

		DialogJst.startLoading(this);
		XUtil.PostJson(url,command,new SmartCallBack<JsonRespose<com.alibaba.fastjson.JSONObject>>(){
			@Override
			public void onSuccess(JsonRespose<JSONObject> result) {
				super.onSuccess(result);
				DialogJst.stopLoading();
				if (result.code > 0) {
					if (cmd1.equals("1709") && result.List != null) {
						adapter.updateList(result.List);
					} else if (cmd1.equals("1708")) {
						adapter.removeItem(guid);
					}
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
				super.onError(ex, isOnCallback);
				DialogJst.stopLoading();
				soundUtil.playAir();
				Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG)
						.show();
			}

			@Override
			public void onFinished() {
				super.onFinished();
				DialogJst.stopLoading();
			}
		});
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == KeyEvent.ACTION_UP) {
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_BACK:
				onBack(null);
				return true;
			default:
				break;
			}
		}
		return super.dispatchKeyEvent(event);
	}
}
